package presenter.equals;

import main.InputBean;
import utils.OnLogListener;
import utils.FileBean;
import utils.FileFilter;
import utils.TextUtils;

import java.util.List;

public class EqualsParser {
    private final String path1;
    private final String path2;
    private final OnLogListener listener;

    public static void check(InputBean info, OnLogListener listener) {
        new EqualsParser(info.path1(), info.path2(), listener);
    }

    private EqualsParser(String path1, String path2, OnLogListener listener) {
        this.path1 = path1;
        this.path2 = path2;
        this.listener = listener;
        check();
    }

    private void check() {
        log("任务开始！！！");
        String md5_1 = md5(path1);
        String md5_2 = md5(path2);
        log(String.format("两个文件比较结果：【%s】", TextUtils.equals(md5_1, md5_2) ? "相同" : "不相同"));
        listener.taskFinish();
    }

    private String md5(String path) {
        List<FileBean> items = FileFilter.filter(true, path, name -> true);
        FileBean item = items.get(0);
        log(String.format("开始文件的md5：%s,稍等...", item.name()));
        item.reKey();
        String md5 = item.md5();
        log(String.format("文件md5完成，md5 = %s", md5));
        return md5;
    }

    private void log(String content) {
        listener.log(content);
    }
}
